package cn.kgc.day04.servlet;

import cn.kgc.day04.entity.Score;
import cn.kgc.day04.http.ResponseResult;
import cn.kgc.day04.service.ScoreService;
import cn.kgc.day04.service.impl.ScoreServiceImpl;
import cn.kgc.day04.utils.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 课工场
 * @date 2024/10/14
 * @description
 */
@WebServlet("/score/layui")
public class ScoreLayUiServlet extends BaseServlet {

    private ScoreService scoreService = new ScoreServiceImpl();

    protected void scorePage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 获取分页数据
        Integer page = Integer.valueOf(req.getParameter("page"));
        Integer limit = Integer.valueOf(req.getParameter("limit"));

        // 调用业务层实现数据分页查询
        ResponseResult responseResult = scoreService.scorePage(page, limit);
        ResponseUtil.writeJson(responseResult, resp);

    }


    // 添加成绩
    protected void saveScore(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 获取成绩信息
        String name = req.getParameter("name");
        String course = req.getParameter("course");
        Integer score = Integer.valueOf(req.getParameter("score"));


        Score scr = new Score();
        scr.setCourse(course);
        scr.setScore(score);
        scr.setName(name);

        // 调用业务层实现数据添加
        ResponseResult responseResult = scoreService.saveScore(scr);
        // 响应json格式数据     html
        ResponseUtil.writeJson(responseResult, resp);
    }

    // 编辑信息
    protected void editScore(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 获取成绩信息
        Integer id = Integer.valueOf(req.getParameter("id"));
        String name = req.getParameter("name");
        String course = req.getParameter("course");
        Integer score = Integer.valueOf(req.getParameter("score"));
        Integer version  = Integer.valueOf(req.getParameter("version"));

        // 封装数据
        Score scr = new Score();
        scr.setCourse(course);
        scr.setScore(score);
        scr.setName(name);
        scr.setId(id);

        // 调用业务层实现数据添加
        ResponseResult responseResult = scoreService.updateScore(scr);
        // 响应json格式数据
        ResponseUtil.writeJson(responseResult, resp);
    }


}
